package com.zjut.azhen.DBDesign.mapping;

import com.zjut.azhen.DBDesign.bean.Course;
import com.zjut.azhen.DBDesign.bean.Report;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
/**
 * @author asus
 */
@Repository
public interface CourseMapper {

    /*
    * TODO:
        4)学生所学课程及；
        7)教师任课查询；
        8)班级课程开设查询。
     * */
    @Results(id="courseResult" ,value={
            @Result(property = "cno" ,column = "Cno" ,id=true),
            @Result(property = "cname" ,column = "Cname"),
            @Result(property = "term" ,column = "Cterm"),
            @Result(property = "cCredit" ,column = "Ccredit"),
            @Result(property = "tname",column = "Tname"),
            @Result(property = "cTest",column = "Ctest"),
            @Result(property = "clName" ,column = "Clname"),
    })



    //传入空值则查询全部terms
    //TODO:debug 违规调用过程

    /**
     * @param sno student num get form session
     * @param term Number of semester
     * @return List<course> 选课结果集
     */
    @Select("{call queryCourseBySno(#{sno ,mode=IN,jdbcType=VARCHAR},#{term,mode=IN,jdbcType=VARCHAR})}")
    List<Course> queryCourseBySno(String sno,String term);

    @Select("select distinct cterm from zhengjy_courses")
    List<String> getTerms();
    @Select("{call QueryCourseTeach(#{tno,mode=IN},#{term,mode=IN})}")
    @ResultMap("courseResult")
    List<Course> queryCourseTeach(@Param("tno") String tno,@Param("term") String term);

    @Select("{call queryCourseByClno(#{clno ,mode=IN},#{term,mode=IN},#{sno ,mode=IN})}")
    @ResultMap("courseResult")
    List<Course> queryCourseByClno(@Param("clno") String clno,@Param("term") String term,@Param("sno") String sno);

    @Select("{call CALWTAVGBYCno(#{term ,mode=IN})}")
    List<Report> getAVG(String term);

    @Insert("insert into zhengjy_sc values(#{sno},#{cno},0) ;")
    Integer inserSC(String sno, String cno);

    @Delete("delete from zhengjy_sc where Sno=#{sno} and Cno=#{cno}")
    Integer deleteSCById(String sno, String cno);


    @Select("select DISTINCT zhengjy_tccl.Cno,Cname,Ccredit,Cterm,Ctime,Ctest,Cterm term,Ctest,cplace from  zhengjy_courses "+
            "inner join zhengjy_classes on zhengjy_courses.Clno =zhengjy_classes.Clno and Clname=#{classname} AND Cterm=#{term}"+
            "inner join zhengjy_tccl on zhengjy_classes.Clno=zhengjy_tccl.Clno and zhengjy_courses.Cno=zhengjy_tccl.Cno group by zhengjy_tccl.Cno")
    List<Course> getByClassName(String classname, String term);


}
